﻿package com.designoutput.ui.controls
{
    import com.designoutput.event.ChangeEvent;
    import com.designoutput.icon.RadiobuttonCheckedHover;
    import com.designoutput.icon.RadiobuttonUncheckedHover;
    
    import flash.display.*;
    import flash.events.*;

    public class Radiobutton extends Sprite
    {
        public var checked:Boolean = false;
        internal var title:Literal;
        internal var uc:IconButton;
        internal var ck:IconButton;

        public function Radiobutton(name:String = "")
        {
            this.uc = new IconButton(new RadiobuttonUncheckedHover(0, 0));
            this.ck = new IconButton(new RadiobuttonCheckedHover(0, 0));
            this.ck.visible = false;
            addChild(this.uc);
            addChild(this.ck);
            if (name != "")
            {
                this.title = new Literal(name);
                this.title.x = 14;
                this.title.y = -1;
                addChild(this.title);
            }
            this.addEventListener(MouseEvent.MOUSE_DOWN, this.MouseDown, false, 1, true);
        }

        private function MouseDown(event:MouseEvent) : void
        {
            this.checked = true;
            this.uc.visible = !this.checked;
            this.ck.visible = this.checked;
            dispatchEvent(new ChangeEvent(this.title.text));
        }

        public function Check(checked:Boolean = true) : void
        {
            this.checked = checked;
            this.uc.visible = !this.checked;
            this.ck.visible = this.checked;
        }

    }
}
